﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace website.Login
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : IHttpHandler
    {
        JavaScriptSerializer jsc = new JavaScriptSerializer();
        string Action = "";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Action = RequstString("Action");

            if (Action.Length == 0) Action = "";

            if (Action == "Login")
            {

                context.Response.Write(jsc.Serialize(GetUserLogin(context)));
            }

            if (Action == "GetUserName")
            {

                context.Response.Write(GetUserName(context));
            }

            if (Action == "EditPsw")
            {

                context.Response.Write(EditPsw());
            }

            //GISDemo获取数据
            if (Action == "GetGISInfo")
            {

                poiList dataEntity;
                dataEntity = new poiList();
               
                dataEntity = getGISInfo(dataEntity);

                context.Response.Write(jsc.Serialize(dataEntity));
            }

        }


       
        public ResultMsg_UserInfo GetUserLogin(HttpContext context)
        {
            string userid = UrlParam("userid");
            string pwd = UrlParam("psw");
            string message = "";
            UserInfo userinfo = UserLoginBLL.GetUserForUName(userid.ToLower());
            if (userinfo == null)
            {
                message = "您输入的用户名并不存在，登录失败！";
                ResultMsg_UserInfo result = new ResultMsg_UserInfo();
                result.data = null;
                result.msg = message;
                result.result = "error";
                return result;
            }
            else if (pwd == userinfo.UserPwd)
            {
                HttpCookie cookie = new HttpCookie("name");
                cookie.Value = userinfo.UserName.ToString();
                HttpCookie cookie1 = new HttpCookie("userno");
                cookie1.Value = userinfo.UserNo.ToString();
                HttpCookie cookie2 = new HttpCookie("CustomerName");
                cookie2.Value = userinfo.CustomerName.ToString();
                HttpCookie cookie3 = new HttpCookie("CustomerID");

                cookie3.Value = (userinfo.CustomerID == null) ? "" : userinfo.CustomerID.ToString();

                context.Response.Cookies.Add(cookie);
                context.Response.Cookies.Add(cookie1);
                context.Response.Cookies.Add(cookie2);
                context.Response.Cookies.Add(cookie3);

                message = "登录成功！";
                ResultMsg_UserInfo result = new ResultMsg_UserInfo();
                result.data = userinfo;
                result.msg = message;
                result.result = "success";
                return result;
               
            }
            else
            {
                message = "密码错误，登录失败！";
                ResultMsg_UserInfo result = new ResultMsg_UserInfo();
                result.data = userinfo;
                result.msg = message;
                result.result = "error";
                return result;
            }

        }

        public string GetUserName(HttpContext context)
        {
            return context.Request.Cookies["name"].Value;
        }

        public string EditPsw()
        {
            return "";
        }

        /// <summary>
        /// 获取GIS数据
        /// </summary>
        /// <param name="dataEntity"></param>
        /// <returns></returns>
        public poiList getGISInfo(poiList dataEntity)
        {
            string keyword = RequstString("keyword");
            string selectstate = RequstString("state");
            string zoom = RequstString("zoom");
            //int pageIndex = Convert.ToInt32(RequstString("pageIndex"));
            //int pageSize = Convert.ToInt32(RequstString("pageSize"));
            string totalcount = string.Empty;
            DataTable dt = new DataTable();
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ELCO_ConnectionString"].ToString()))
            {
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                SqlParameter[] sqlPara = new SqlParameter[3];

                sqlPara[0] = new SqlParameter("@keyword", keyword);
                sqlPara[0].Direction = ParameterDirection.Input;
                sqlPara[1] = new SqlParameter("@selectstate", selectstate);
                sqlPara[1].Direction = ParameterDirection.Input;
                sqlPara[2] = new SqlParameter("@zoom", zoom);
                sqlPara[2].Direction = ParameterDirection.Input;

                //sqlPara[1] = new SqlParameter("@begin", (pageIndex - 1) * pageSize);
               // sqlPara[1].Direction = ParameterDirection.Input;
               // sqlPara[2] = new SqlParameter("@end", pageIndex * pageSize);
               // sqlPara[2].Direction = ParameterDirection.Input;
               // sqlPara[3] = new SqlParameter("@totalcount", totalcount);
               // sqlPara[3].Size = 10;
               // sqlPara[3].Direction = ParameterDirection.InputOutput;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_GISInfo";
                foreach (SqlParameter para in sqlPara)
                {
                    cmd.Parameters.Add(para);
                }

                cmd.ExecuteNonQuery();
                SqlDataAdapter Datapter = new SqlDataAdapter(cmd);
                Datapter.Fill(dt);
                poiList poilist = new poiList();
                //poilist.pageIndex = pageIndex.ToString();
                //poilist.pageSize = pageSize.ToString();
                //poilist.count = sqlPara[3].Value.ToString();
                List<pois> poislist = new List<pois>();
                List<location> locationlist;
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        pois pois = new pois();
                        location location = new location();
                        pois.ID = dt.Rows[i]["ID"].ToString();
                        pois.CustomName = dt.Rows[i]["CustomName"].ToString();
                        pois.CustomDetailAddress = dt.Rows[i]["CustomDetailAddress"].ToString();
                        pois.CustomeState = dt.Rows[i]["CustomeState"].ToString();
                        pois.InstallState = dt.Rows[i]["InstallState"].ToString();

                        location.lat = dt.Rows[i]["CustomLat"].ToString();
                        location.lng = dt.Rows[i]["CustomLng"].ToString();
                        locationlist = new List<location>();
                        locationlist.Add(location);
                        pois.location = locationlist;
                        poislist.Add(pois);
                        poilist.pois = poislist;
                        dataEntity = poilist;
                    }

                }

            }
            return dataEntity;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public class ResultMsg_UserInfo
        {
            public string result { set; get; }
            public string msg { set; get; }
            public UserInfo data { set; get; }
        }

        public static string RequstString(string sParam)
        {
            return (HttpContext.Current.Request[sParam] == null ? string.Empty
                  : HttpContext.Current.Request[sParam].ToString().Trim());
        }

        public string UrlParam(string Param)
        {
            if (HttpContext.Current.Request[Param] == null)
            {
                return "";
            }
            else
            {
                return HttpContext.Current.Request[Param].ToString();
            }
        }


    }
}